package com.zhouhong;

/**
 * @ClassName: LinearSearch
 * @Description: 线性查找
 * @Author: zhouhong
 * @Create: 2021-03-28 13:08
 **/

public class LinearSearch {
    private LinearSearch(){
    };
    /** 
    * @Description: 使用普通循环遍历实现
    * @Param: [data, target]
    * @return: int
    * @Author: zhouhong
    * @Date: 2021/3/28
    */
    public static <E> int search(E[] data, E target){
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals(target)){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {

        int[] dataSize = {1000000, 10000000};
        for (int n: dataSize) {
            Integer[] data = ArrayGenerator.generatorOrderedArray(n);

            long startTime = System.nanoTime();
            // 进行100次查找
            for (int i = 0; i < 100; i++) {
                LinearSearch.search(data, n);
            }
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("数据规模为" + n + ",执行100次查找，耗费时间为Time：" + time + "秒");
        }
    }
}